package com.aconcagua.beans {
  import com.aconcagua.factory.BeanFactory;
  
  [DefaultProperty("ref")]
  public class Ref implements ValueHolder {
    public var ref: Object; //BeanDefinitionFactory;
    
/*     public function Ref(bean: BeanDefinitionFactory = null) { */
    public function Ref(bean: Object = null) {
      ref = bean;
    }

    //TODO: AFP: Split support classes from factory classes
    public function getValue(beanFactory: BeanFactory): Object {
      if (ref is BeanDefinitionFactory) {
        if (!ref.isRegisteredIn(beanFactory)) {
          ref.registerIn(beanFactory);
        }
        
        return ref.retrieveFrom(beanFactory);
      }
      else if (ref is BeanDefinition) {
        var definition: BeanDefinition = ref as BeanDefinition;
        if (!beanFactory.containsBean(definition.name)) {
          beanFactory.registerBeanDefinition(definition);
        }
        
        return beanFactory.getBean(definition.name);
      }
      else {
        throw new Error("Unexpected object");
      }
/*       if (!beanFactory.containsBean(ref.name)) {
        ref.registerIn(beanFactory);
      }

      return beanFactory.getBean(ref.name);
 */    }
  }
}